package com.rdExpenses.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.base.common.persistence.Page;
import com.base.util.JsonUtil;
import com.company.model.CompanyDevice;
import com.company.model.CompanyProject;
import com.company.service.ProjectManageService;
import com.rdExpenses.model.ExpenseAssign;
import com.rdExpenses.model.ExpenseAssignItem;
import com.rdExpenses.service.ExpenseAssignItemService;
import com.rdExpenses.service.ExpenseAssignService;

@Controller
public class ExpenseProjectController {
	// 页面actionView的地址
	public static final String toExpenseAssignView = "/expenseAssign";
	public static final String expenseAssignList = "/expenseAssign/list";
	public static final String toExpenseAssignItem = "/expenseAssign/item";
	public static final String expenseAssignSave = "/expenseAssign/save";

	// 页面路径
	public static final String expenseAssignPage = "/rdExpenses/projectExpenseAssign";
	public static final String expenseAssignListPage = "/rdExpenses/projectExpenseAssignList";
	public static final String expenseAssignItemPage = "/rdExpenses/expenseAssign";

	@Autowired
	ProjectManageService projectManageService;
	@Autowired
	ExpenseAssignItemService expenseAssignItemService;
	@Autowired
	ExpenseAssignService expenseAssignService;

	@RequestMapping(value = toExpenseAssignView, method = RequestMethod.POST)
	public ModelAndView toCompanyView(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(expenseAssignPage);
		String companyId = req.getSession().getAttribute("companyId").toString();
		String userId = req.getSession().getAttribute("userId").toString();
		List<CompanyProject> list = null;
		List<ExpenseAssign> expenseAssignList = null;
		try {
			list = projectManageService.findProjectByCompanyId(companyId);
			expenseAssignList = expenseAssignService.findExpenseAssignListByUserId(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("projectList", list);
		mv.addObject("expenseAssignList", expenseAssignList);
		return mv;
	}
	
	@RequestMapping(value = expenseAssignList, method = RequestMethod.POST)
	public ModelAndView expenseAssignList(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(expenseAssignListPage);
		String userId = req.getSession().getAttribute("userId").toString();
		List<ExpenseAssign> expenseAssignList = null;
		try {
			expenseAssignList = expenseAssignService.findExpenseAssignListByUserId(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("expenseAssignList", expenseAssignList);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = toExpenseAssignItem, method = RequestMethod.POST)
	public ModelAndView toExpenseAssignItem(HttpServletRequest req, HttpServletResponse res, String assignId) {
		ModelAndView mv = new ModelAndView(expenseAssignItemPage);
		List<ExpenseAssignItem> list = null;
		List<CompanyProject> projectList = null;
		String companyId = req.getSession().getAttribute("companyId").toString();
		try {
			list = expenseAssignItemService.findExpenseAssignItemByAssignId(assignId);
			projectList = projectManageService.findProjectByCompanyId(companyId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (list != null && list.size() > 0) {
			mv.addObject("expenseAssignItem", list);
		} else {
			mv.addObject("projectList", projectList);
		}
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = expenseAssignSave, method = RequestMethod.POST)
	public boolean save(String assignId, String info) {
		boolean result = false;
		try {
			if (info != null && info.length() > 0) {
				expenseAssignItemService.deleteExpenseAssign(assignId);
				String[] a = info.split(";");
				if (a.length > 0) {
					for (String b : a) {
						ExpenseAssignItem expenseAssignItem = new ExpenseAssignItem();
						expenseAssignItem.setExpenseAssignId(assignId);
						String[] c = b.split(":");
						expenseAssignItem.setProjectId(c[0]);
						if (c.length == 2) {
							expenseAssignItem.setRate(Double.parseDouble(b.split(":")[1]));
						} 
						expenseAssignItemService.saveExpenseAssign(expenseAssignItem);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
